package cn.edu.experiment3.entity;

import java.util.ArrayList;
import java.util.List;
import cn.edu.experiment3.base.Person;

public class Student extends Person {
    private final List<Course> courses = new ArrayList<>();

    public Student(String id, String name) { super(id, name); }

    @Override
    public void printSchedule() {
        System.out.println("========== 学生个人课表 ==========");
        System.out.println("编号：" + id);
        System.out.println("姓名：" + name);
        System.out.println("课程名称：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getName).reduce((a, b) -> a + "，" + b).orElse("暂无")));
        System.out.println("上课地点：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getLocation).reduce((a, b) -> a + "，" + b).orElse("暂无")));
        System.out.println("时间：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(Course::getTime).reduce((a, b) -> a + "，" + b).orElse("暂无")));
        System.out.println("授课教师：" + (courses.isEmpty() ? "暂无" :
                courses.stream().map(c -> c.getTeacher() == null ? "待定" : c.getTeacher().getName())
                        .reduce((a, b) -> a + "，" + b).orElse("暂无")));
    }

    public void select(Course c) {
        if (c == null) return;
        for (Course course : courses)
            if (course.getId().equals(c.getId())) {
                System.out.println("学生 " + name + " 已选过 " + c.getName() + "，忽略");
                return;
            }
        courses.add(c);
        System.out.println("学生 " + name + " 选课：" + c.getName());
    }

    public void drop() {
        if (courses.isEmpty()) return;
        Course c = courses.remove(0);
        System.out.println("学生 " + name + " 退课：" + c.getName());
    }
}